<template>
  <el-dialog
    :title="!dataForm.visitorIdCard ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="参观者编号" prop="visitorNo">
      <el-input v-model="dataForm.visitorNo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="参观者证件类型" prop="visitorIdCardType">
      <el-input v-model="dataForm.visitorIdCardType" placeholder=""></el-input>
    </el-form-item>
    <!-- <el-form-item label="参观者性别" prop="visitorSex">
      <el-input v-model="dataForm.visitorSex" placeholder=""></el-input>
    </el-form-item> -->
    <el-form-item label="参观者姓名" prop="visitorName">
      <el-input v-model="dataForm.visitorName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="参观者电话" prop="visitorPhone">
      <el-input v-model="dataForm.visitorPhone" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="点赞文物id" prop="likeExhibitIds">
      <el-input v-model="dataForm.likeExhibitIds" placeholder=""></el-input>
    </el-form-item>
    <!-- <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getVisitorById, saveOrUpdateVisitor } from '@/api/visitor'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        visitorNo: '',
        visitorIdCard: 0,
        visitorIdCardType: '',
        visitorSex: '',
        visitorName: '',
        visitorPhone: '',
        likeExhibitIds: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        visitorNo: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        visitorIdCardType: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        visitorSex: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        visitorName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        visitorPhone: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        likeExhibitIds: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.visitorIdCard = id || 0
      this.visible = true
      this.$nextTick(async () => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.visitorIdCard) {
          const { data } = await getVisitorById(this.dataForm.visitorIdCard)
          if (data && data.code === 0) {
            this.dataForm.visitorNo = data.visitor.visitorNo
            this.dataForm.visitorIdCardType = data.visitor.visitorIdCardType
            this.dataForm.visitorSex = data.visitor.visitorSex
            this.dataForm.visitorName = data.visitor.visitorName
            this.dataForm.visitorPhone = data.visitor.visitorPhone
            this.dataForm.likeExhibitIds = data.visitor.likeExhibitIds
            this.dataForm.createTime = data.visitor.createTime
            this.dataForm.updateTime = data.visitor.updateTime
          }

          // this.$http({
          //   url: `/visitor/info/${this.dataForm.visitorIdCard}`,
          //   method: 'get'
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.dataForm.visitorNo = data.visitor.visitorNo
          //     this.dataForm.visitorIdCardType = data.visitor.visitorIdCardType
          //     this.dataForm.visitorSex = data.visitor.visitorSex
          //     this.dataForm.visitorName = data.visitor.visitorName
          //     this.dataForm.visitorPhone = data.visitor.visitorPhone
          //     this.dataForm.likeExhibitIds = data.visitor.likeExhibitIds
          //     this.dataForm.createTime = data.visitor.createTime
          //     this.dataForm.updateTime = data.visitor.updateTime
          //   }
          // })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs.dataForm.validate(async (valid) => {
        if (valid) {
          const visitor = {
            visitorNo: this.dataForm.visitorNo,
            visitorIdCard: this.dataForm.visitorIdCard || undefined,
            visitorIdCardType: this.dataForm.visitorIdCardType,
            visitorSex: this.dataForm.visitorSex,
            visitorName: this.dataForm.visitorName,
            visitorPhone: this.dataForm.visitorPhone,
            likeExhibitIds: this.dataForm.likeExhibitIds,
            createTime: this.dataForm.createTime,
            updateTime: this.dataForm.updateTime
          }
          const { data } = await saveOrUpdateVisitor(this.dataForm.visitorIdCard, visitor)
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }

          // this.$http({
          //   url: `/visitor/${!this.dataForm.visitorIdCard ? 'save' : 'update'}`,
          //   method: 'post',
          //   data: {
          //     visitorNo: this.dataForm.visitorNo,
          //     visitorIdCard: this.dataForm.visitorIdCard || undefined,
          //     visitorIdCardType: this.dataForm.visitorIdCardType,
          //     visitorSex: this.dataForm.visitorSex,
          //     visitorName: this.dataForm.visitorName,
          //     visitorPhone: this.dataForm.visitorPhone,
          //     likeExhibitIds: this.dataForm.likeExhibitIds,
          //     createTime: this.dataForm.createTime,
          //     updateTime: this.dataForm.updateTime
          //   }
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.$message({
          //       message: '操作成功',
          //       type: 'success',
          //       duration: 1500,
          //       onClose: () => {
          //         this.visible = false
          //         this.$emit('refreshDataList')
          //       }
          //     })
          //   } else {
          //     this.$message.error(data.msg)
          //   }
          // })
        }
      })
    }
  }
}
</script>
